package com.lq.demo.demo.mode.mycache;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

public class CacheManagerImpl implements ICacheManager {
    public  int  maxSize=2000;
    private static final Map<Object, EntitiCache> caches =new ConcurrentHashMap<>();

    @Override
    public void putCache(String key, EntitiCache entitiCache) {
        if(caches.size()>maxSize)caches.clear();

        caches.put(key, entitiCache);
    }

    @Override
    public void putCache(String key, Object data) {
        if(caches.size()>maxSize)caches.clear();

        caches.put(key, new EntitiCache<>(data, System.currentTimeMillis()));

    }

    @Override
    public void putCache(Number key, EntitiCache entitiCache) {
        if(caches.size()>maxSize)caches.clear();

        caches.put(key, entitiCache);
    }

    @Override
    public void putCache(Number key, Object data) {
        if(caches.size()>maxSize)caches.clear();

        caches.put(key, new EntitiCache<>(data, System.currentTimeMillis()));

    }

    @Override
    public void putCache(Object key, EntitiCache entitiCache) {
        if(caches.size()>maxSize)caches.clear();

        caches.put(key, entitiCache);

    }

    @Override
    public void putCache(Object key, Object data) {
        if(caches.size()>maxSize)caches.clear();
        caches.put(key, new EntitiCache<>(data, System.currentTimeMillis()));

    }

    @Override
    public EntitiCache getCacheByKey(String key) {
        if (this.isContains(key)) {
            return (EntitiCache) caches.get(key);
        }
        return null;
    }

    @Override
    public EntitiCache getCacheByKey(Number key) {
        if (this.isContains(key)) {
            return (EntitiCache) caches.get(key);
        }
        return null;
    }

    @Override
    public EntitiCache getCacheByKey(Object key) {
        if (this.isContains(key)) {
            return (EntitiCache) caches.get(key);
        }
        return null;
    }

    @Override
    public Object getCacheDataByKey(String key) {
        if (isContains(key)) {
            return caches.get(key).getData();
        }
        return null;
    }

    @Override
    public Object getCacheDataByKey(Number key) {
        if (isContains(key)) {
            return caches.get(key).getData();
        }
        return null;
    }

    @Override
    public Object getCacheDataByKey(Object key) {
        if (isContains(key)) {
            return caches.get(key).getData();
        }
        return null;
    }

    @Override
    public void clearAll() {
        caches.clear();
    }

    @Override
    public void clearByKey(String key) {
        if (isContains(key)) {
            caches.remove(key);
        }
    }

    @Override
    public void clearByKey(Number key) {
        if (isContains(key)) {
            caches.remove(key);
        }
    }

    @Override
    public void clearByKey(Object key) {
        if (isContains(key)) {
            caches.remove(key);
        }
    }

    @Override
    public Map<Object, EntitiCache> getAllCache() {
        return caches;
    }

    @Override
    public Set getAllKeys() {
        return caches.keySet();
    }

    @Override
    public boolean isContains(String key) {
        return caches.containsKey(key);
    }

    @Override
    public boolean isContains(Number key) {
        return caches.containsKey(key);
    }

    @Override
    public boolean isContains(Object key) {
        return caches.containsKey(key);
    }

    @Override
    public boolean isTimeOut(String key) {
        return  judge(key);
    }

    @Override
    public boolean isTimeOut(Number key) {
        return  judge(key);
    }

    @Override
    public boolean isTimeOut(Object key) {
        return judge(key);
    }

    @Override
    public Integer getSize() {
        return caches.size();
    }

    private boolean  judge(Object key){
        if (!isContains(key)) {
            return true;
        }

        EntitiCache entitiCache = caches.get(key);
        long timeOut = entitiCache.getTimeOut();
        long refreshTime = entitiCache.getRefreshTime();
        if (timeOut == 0 || (System.currentTimeMillis() - refreshTime) >= timeOut) {
            return true;
        }
        return false;
    }
}
